﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace KZ400B.Controls.TPlan
{
    /// <summary>
    /// TPlanAdd.xaml 的交互逻辑
    /// </summary>
    public partial class TPlanAdd : UserControl
    {
        public event UserControlClose OnClose;
        public TPlanAdd()
        {
            InitializeComponent();
        }

        TPlanManager m_tManager = new TPlanManager();
        DB.Model.TPlanAxle getAxleModel(TPlanAxle control)
        {
            DB.Model.TPlanAxle ax = null;
            if (control != null && control.IsEnabled)
            {
                ax = new DB.Model.TPlanAxle();
                ax.StartPosition = control.StartPostion;
                //ax.RunUnit = (int)control.RunningUnit;
                ax.AxleName = control.AxleName;
                ax.AxleID = control.AxleID;
            }
            return ax;
        }

        

        private int m_TPlanID = 0;
        public int TPlanID
        {
            get { return (int)m_TPlanID; }
            
        }

        private void btnSave_Click(object sender, RoutedEventArgs e)
        {
            if (string.IsNullOrEmpty(txtTPName.Text))
            {
                MessageBox.Show("请输入方案名称");
                return;
            }

            if (string.IsNullOrEmpty(txtTotalLoopTimes.Text))
            {
                MessageBox.Show("请输入总循环次数");
                return;
            }

            DB.Model.TPlan plan = new DB.Model.TPlan();
            plan.Name = txtTPName.Text;
            int iValue = 0;
            if (int.TryParse(txtTotalLoopTimes.Text, out iValue))
            {
                plan.LoopTimes = iValue;
            }
            List<DB.Model.TPlanAxle> aList = new List<DB.Model.TPlanAxle>();
            aList.Add(getAxleModel(xControl));
            aList.Add(getAxleModel(yControl));
            aList.Add(getAxleModel(zControl));
            aList.Add(getAxleModel(tControl));
            aList.Add(getAxleModel(uControl));
            aList.Add(getAxleModel(wControl));
            try
            {
                int rst = m_tManager.add(plan, aList);
                if (rst > 0)
                {
                    m_TPlanID = rst;
                }
                MessageBox.Show(string.Format("添加{0}", rst > 0 ? "成功" : "失败"));
               
            }
            catch (Exception ex)
            {
                MessageBox.Show(string.Format("添加失败:{0}", ex.Message));
            }
        }

        private void btnClose_Click(object sender, RoutedEventArgs e)
        {
            if (OnClose != null)
            {
                OnClose();
            }
        }
    }
}
